package com.example.ordersystem.utils;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;

import android.widget.TextView;
import android.widget.Toast;

import com.example.ordersystem.MainActivity;
import com.example.ordersystem.R;
import com.example.ordersystem.pojo.Food;
import com.example.ordersystem.pojo.Store;


import java.util.HashMap;
import java.util.List;

//收藏菜品展示

public class LikeFoodAdapter extends ArrayAdapter {
    private final int resourceId;

    public LikeFoodAdapter(Context context, int textViewResourceId, List<Food> listItems) {

        super(context, textViewResourceId, listItems);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Food food = (Food) getItem(position); // 获取当前项的Food实例

        System.out.println(food);

        final View view = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象
        ImageView foodImage = view.findViewById(R.id.food_image1);//获取该布局内的图片视图
        TextView foodName = view.findViewById(R.id.food_name1);
        TextView foodMessage = view.findViewById(R.id.food_message1);
        TextView storeName = view.findViewById(R.id.store_name1);
        TextView foodprice = view.findViewById(R.id.food_price1);
        Button btn_dislike = view.findViewById(R.id.btn_dislike);
        Button btn_to_order = view.findViewById(R.id.btn_to_order);

        String storeName1 = null;
        foodImage.setImageResource(food.getImageId());//为图片视图设置图片资源

        List<Store> storeList = ListDataSave.getStoreList(getContext());
        for (Store store : storeList) {
            if (food.getStoreId() == store.getStoreId()) {
                storeName1 = store.getStoreName();
            }
        }

        storeName.setText("店铺名：" + storeName1);//为文本视图设置文本内容
        foodName.setText("菜品名：" + food.getFoodName());
        foodMessage.setText("菜品简介：" + food.getMessage());
        foodprice.setText("菜品价格：" + food.getPrice());
        btn_dislike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Like_Food.deleteFood(food);
                view.setVisibility(View.GONE);
                Toast.makeText(getContext(), "已取消收藏", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getContext(), MainActivity.class);
                boolean dislike = true;
                intent.putExtra("dislike", dislike);
                getContext().startActivity(intent);

            }
        });

        btn_to_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, List<Food>> map = ListDataSave.getUserFoodOrderList(getContext(), ListDataSave.getUserLogin(getContext()));
                Order_Food.addFood(food);
                Toast.makeText(getContext(), "已经加入订单", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getContext(), MainActivity.class);
                boolean dislike = true;
                intent.putExtra("dislike", dislike);
                getContext().startActivity(intent);
            }
        });

        return view;
    }


}


